package com.fzy.core.codetools;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

public class Utils {
	private static long randomSeed = 0L;
	private static String BOOLEAN_TRUE = "true";
	private static String BOOLEAN_FALSE = "false";
	
	
	public static void deleteFileDeeply(File file){
		if(!file.exists()) return;
		if(file.isFile()){
			file.delete();
			return;
		} 
		if(file.isDirectory()){
			File[] files = file.listFiles();
			if(files != null && files.length > 0){
				for (int i = 0; i < files.length; i++) {
					deleteFileDeeply(files[i]);
				}
			}
			file.delete();
		}
	}
	
	public static String escapeXml(String value){
		if(value == null) return null;
		String result = "";
        for (int i = 0; i < value.length(); i++) {
            String temp = value.substring(i, i + 1);
            if (isAssic(temp)) {
                result += StringEscapeUtils.escapeXml(temp);
            } else {
            	result += temp;
            }
        }
		return result;
	}
	
    public static boolean isAssic(String src){
    	return src == null ? false : src.matches("[\u0000-\u00ff]+");
    }

	/**是否为保留字段
	 * @return
	 */
	public static boolean isSavedProperty(String propertyName){
		boolean result = false;
		String[] l = new String[]{"id", "createdUser", "createdDate", "updatedUser", "updatedDate", "status"};
		for (int i = 0; i < l.length; i++) {
			String s = l[i];
			if(s.equals(propertyName)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public static String obj2xmlAttr(Object o) {
		String result = "";
		List destFields = Utils.getFields(o.getClass());

		for (Iterator iterator2 = destFields.iterator(); iterator2.hasNext();) {
			Field destField = (Field) iterator2.next();
			String fieldName = destField.getName();
			destField.setAccessible(true);
			try {
				String value = (String) destField.get(o);
				if (value != null) {
					result += " " + camel2hyphen(fieldName) + "=\"" + value
							+ "\"";
				}
			} catch (IllegalArgumentException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			}
		}
		return result;
	}

	public static String hello(String name) {
		return "hello " + name + " !";
	}

	public static String getRandomLong() {
		long seed = randomSeed++ + System.currentTimeMillis();
		Random r = new Random(seed);
		return r.nextLong() + "L";
	}

	public static String camel2underline(String src) {
		String result = "";
		for (int i = 0; i < src.length(); i++) {
			char c = src.charAt(i);
			if (Character.isUpperCase(c) && i > 0)
				result += "_" + c;
			else
				result += c;
		}
		return result.toLowerCase();
	}

	public static String camel2hyphen(String src) {
		String result = "";
		for (int i = 0; i < src.length(); i++) {
			char c = src.charAt(i);
			if (Character.isUpperCase(c) && i > 0)
				result += "-" + c;
			else
				result += c;
		}
		return result.toLowerCase();
	}

	public static boolean string2boolean(String src, boolean defaultValue) {
		boolean result = defaultValue;
		if (StringUtils.isBlank(src))
			return result;
		src = StringUtils.trim(src).toLowerCase();
		if (BOOLEAN_TRUE.equals(src))
			result = true;
		if (BOOLEAN_FALSE.equals(src))
			result = false;
		return result;
	}

	public static Long string2longObj(String value) {
		if (value == null) {
			return null;
		}

		try {
			return new Long(value);
		} catch (NumberFormatException e) {
			return null;
		}
	}

	public static Integer string2intObj(String value) {
		if (value == null) {
			return null;
		}

		try {
			return new Integer(value);
		} catch (NumberFormatException e) {
			return null;
		}
	}

	public static Double string2doubleObj(String value) {
		if (value == null) {
			return null;
		}

		try {
			return new Double(value);
		} catch (NumberFormatException e) {
			return null;
		}
	}

	public static int string2int(String src, int defaultValue) {
		Integer i = string2intObj(src);
		if (i == null)
			return defaultValue;
		return i.intValue();
	}

	public static long string2long(String src, long defaultValue) {
		Long l = string2longObj(src);
		if (l == null)
			return defaultValue;
		return l.longValue();
	}

	public static String string2unicode(final String src) {
		char[] utfBytes = src.toCharArray();
		String unicodeBytes = "";
		for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
			String hexB = Integer.toHexString(utfBytes[byteIndex]);
			if (hexB.length() <= 2) {
				hexB = "00" + hexB;
			}
			unicodeBytes = unicodeBytes + "\\u" + hexB.toUpperCase();
		}
		return unicodeBytes;
	}

	public static Object getFieldValue(final Object obj, final String fieldName) {
		Field field = getField(obj.getClass(), fieldName);
		if (field != null) {
			field.setAccessible(true);
			try {
				return field.get(obj);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static void setFieldValue(final Object obj, final String fieldName,
			final Object value) {
		Field field = getField(obj.getClass(), fieldName);
		if (field != null) {
			field.setAccessible(true);
			try {
				field.set(obj, value);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	public static Field getField(Class objClass, final String fieldName) {
		for (Class superClass = objClass; superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			try {
				Field field = superClass.getDeclaredField(fieldName);
				return field;
			} catch (NoSuchFieldException e) {// NOSONAR
			}
		}
		return null;
	}

	public static List getFields(Class objClass) {
		List result = new ArrayList();
		for (Class superClass = objClass; superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			Field[] fields = superClass.getDeclaredFields();
			addFieldsToList(fields, result);
		}
		return result;
	}

	private static void addFieldsToList(Field[] fields, List list) {
		if (fields == null || list == null)
			return;
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			String fieldName = f.getName();
			boolean isInList = false;
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Field field = (Field) iterator.next();
				if (fieldName.equals(field.getName())) {
					isInList = true;
					break;
				}
			}
			if (!isInList)
				list.add(f);
		}
	}

}
